//! 配置管理模块
//! 
//! 提供系统配置和服务配置管理功能

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// 系统配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemConfig {
    /// 引导服务列表
    pub boot_services: Vec<String>,
    /// 服务配置映射
    pub services: HashMap<String, ServiceConfig>,
    /// 日志级别
    pub logging_level: String,
    /// 最大进程数
    pub max_processes: u32,
    /// 内存限制（MB）
    pub memory_limit_mb: u64,
}

/// 服务配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceConfig {
    /// 是否启用
    pub enabled: bool,
    /// 服务优先级
    pub priority: u32,
    /// 最大内存使用（MB）
    pub max_memory_mb: u64,
    /// 是否自动启动
    pub auto_start: bool,
    /// 所需能力列表
    pub capabilities: Vec<String>,
}

impl Default for SystemConfig {
    fn default() -> Self {
        let mut services = HashMap::new();
        
        // 内存服务配置
        services.insert("memory".to_string(), ServiceConfig {
            enabled: true,
            priority: 50,
            max_memory_mb: 4096,
            auto_start: true,
            capabilities: vec!["memory".to_string()],
        });
        
        // 进程服务配置
        services.insert("process".to_string(), ServiceConfig {
            enabled: true,
            priority: 60,
            max_memory_mb: 256,
            auto_start: true,
            capabilities: vec!["cpu".to_string()],
        });
        
        // 文件系统服务配置
        services.insert("filesystem".to_string(), ServiceConfig {
            enabled: true,
            priority: 70,
            max_memory_mb: 1024,
            auto_start: true,
            capabilities: vec!["storage".to_string()],
        });
        
        // 网络服务配置
        services.insert("network".to_string(), ServiceConfig {
            enabled: true,
            priority: 80,
            max_memory_mb: 128,
            auto_start: true,
            capabilities: vec!["network".to_string()],
        });
        
        // 图形服务配置
        services.insert("graphics".to_string(), ServiceConfig {
            enabled: true,
            priority: 90,
            max_memory_mb: 512,
            auto_start: true,
            capabilities: vec!["graphics".to_string()],
        });
        
        // 桌面环境服务配置
        services.insert("desktop_environment".to_string(), ServiceConfig {
            enabled: true,
            priority: 100,
            max_memory_mb: 256,
            auto_start: true,
            capabilities: vec!["desktop".to_string()],
        });
        
        // Shell服务配置
        services.insert("shell".to_string(), ServiceConfig {
            enabled: true,
            priority: 110,
            max_memory_mb: 128,
            auto_start: true,
            capabilities: vec!["shell".to_string()],
        });
        
        Self {
            boot_services: vec![
                "memory".to_string(),
                "process".to_string(),
                "filesystem".to_string(),
                "graphics".to_string(),
                "desktop_environment".to_string(),
                "shell".to_string(),
            ],
            services,
            logging_level: "info".to_string(),
            max_processes: 128,
            memory_limit_mb: 4096,
        }
    }
}

impl SystemConfig {
    /// 从JSON字符串加载配置
    pub fn from_json(json_str: &str) -> Result<Self, serde_json::Error> {
        serde_json::from_str(json_str)
    }
    
    /// 从TOML字符串加载配置
    pub fn from_toml(toml_str: &str) -> Result<Self, toml::de::Error> {
        toml::from_str(toml_str)
    }
    
    /// 将配置保存为JSON字符串
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }
    
    /// 将配置保存为TOML字符串
    pub fn to_toml(&self) -> Result<String, toml::ser::Error> {
        toml::to_string_pretty(self)
    }
    
    /// 获取服务配置
    pub fn get_service_config(&self, service_name: &str) -> Option<&ServiceConfig> {
        self.services.get(service_name)
    }
    
    /// 检查服务是否启用
    pub fn is_service_enabled(&self, service_name: &str) -> bool {
        self.services.get(service_name)
            .map(|config| config.enabled)
            .unwrap_or(false)
    }
    
    /// 获取引导服务列表
    pub fn get_boot_services(&self) -> &[String] {
        &self.boot_services
    }
}

impl ServiceConfig {
    /// 检查服务是否具有指定能力
    pub fn has_capability(&self, capability: &str) -> bool {
        self.capabilities.iter().any(|c| c == capability)
    }
}